package com.itcm.skymail.ware.controller;

import com.itcm.common.utils.PageUtils;
import com.itcm.common.utils.R;
import com.itcm.skymail.ware.entity.PurchaseEntity;
import com.itcm.skymail.ware.entity.vo.FinishVo;
import com.itcm.skymail.ware.entity.vo.MergeVo;
import com.itcm.skymail.ware.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 采购信息
 *
 * @author cm_fighting
 * @email cmtianxie@163.com
 * @date 2020-08-27 17:03:07
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 完成采购单：
     * <p>
     * 这里管理页面没有这个入口 应该是app来做完成采购单操作 在每个采购单中标记每个采购单详情的状态
     * 如果失败的 还需要注明原因
     * 测试可以使用postman等测试接口工具
     */
    @PostMapping("done")
    public R finish(@RequestBody FinishVo vo) {
        purchaseService.finish(vo);
        return R.ok();
    }

    /**
     * 领取采购单：
     * <p>
     * 这里管理页面没有这个入口 应该是app来做领取采购单操作
     * 测试可以使用postman等测试接口工具
     */
    @PostMapping("received")
    public R received(@RequestBody List<Long> purchaseIds) {
        purchaseService.received(purchaseIds);
        return R.ok();
    }

    /**
     * 合并整单
     * items: [1, 2]
     * purchaseId: 1
     */
    @PostMapping("merge")
    public R merge(@RequestBody MergeVo vo) {
        purchaseService.merge(vo);
        return R.ok();
    }

    /**
     * purchase/unreceive/list
     * 获取未分配的采购单
     */
    @GetMapping("/unreceive/list")
    public R listUnreceive(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.listUnreceive(params);
        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseEntity purchase) {
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());
        purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PurchaseEntity purchase) {
        purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
